package com.util;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import org.apache.log4j.Logger;

public class UrlUtil {
	private static final Logger LOG = Logger.getLogger(UrlUtil.class);

	public static String encode(String str) {
		try {
			return URLEncoder.encode(str, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			LOG.error("no such charset for encoding !", e);
		}
		return null;
	}

	public static String encode(String str, String charsetName) {
		try {
			return URLEncoder.encode(str, charsetName);
		} catch (UnsupportedEncodingException e) {
			LOG.error("no such charset for encoding !", e);
		}
		return null;
	}

	public static String decode(String str) {
		try {
			return URLDecoder.decode(str, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			LOG.error("no such charset for encoding !", e);
		}
		return null;
	}

	public static String decode(String str, String charsetName) {
		try {
			return URLDecoder.decode(str, charsetName);
		} catch (UnsupportedEncodingException e) {
			LOG.error("no such charset for encoding !", e);
		}
		return null;
	}
}